{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import re\n",
    "import json\n",
    "import azureml\n",
    "import requests\n",
    "import argparse\n",
    "from pathlib import Path\n",
    "from azureml.core.run import Run\n",
    "from azureml.core import Workspace\n",
    "from azureml.core.model import Model\n",
    "from azureml.core.image import ContainerImage, Image\n",
    "from azureml.core.conda_dependencies import CondaDependencies\n",
    "from azureml.core.webservice import Webservice, AciWebservice\n",
    "\n",
    "print(\"Azure ML SDK Version: \", azureml.core.VERSION)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ws = Workspace.from_config()\n",
    "model = ws.models['seer']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "seer_env = CondaDependencies()\n",
    "seer_env.add_pip_package('tensorflow')\n",
    "seer_env.add_pip_package('numpy')\n",
    "seer_env.add_pip_package('pillow')\n",
    "seer_env.add_pip_package('requests')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scoring_yml = 'seer_scoring.yml'\n",
    "with open(scoring_yml,'w') as f:\n",
    "    print('Writing out {}'.format(scoring_yml))\n",
    "    f.write(seer_env.serialize_to_string())\n",
    "    print('Done!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aciconfig = AciWebservice.deploy_configuration(cpu_cores=2,\n",
    "                                               memory_gb=4,\n",
    "                                               description='Transfer learning image classification')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# configure the image\n",
    "image_config = ContainerImage.image_configuration(execution_script='score.py', \n",
    "                                                  runtime='python', \n",
    "                                                  conda_file=scoring_yml)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "service_name = 'seer-svc'\n",
    "if service_name in ws.webservices:\n",
    "    ws.webservices[service_name].delete()\n",
    "    \n",
    "service = Webservice.deploy_from_model(workspace=ws,\n",
    "                                       name=service_name,\n",
    "                                       deployment_config=aciconfig,\n",
    "                                       models=[model],\n",
    "                                       image_config=image_config)\n",
    "\n",
    "service.wait_for_deployment(show_output=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('deploy.log','w') as f:\n",
    "    f.write(service.get_logs())\n",
    "scoring_uri = service.scoring_uri"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tacos = 'https://lh3.googleusercontent.com/-UT5H8nPflkQ/T4tqueyhb_I/AAAAAAAAGl8/1FP7G__Zuys/s640/Lentil+Tacos+close.jpg'\n",
    "r = requests.post(scoring_uri, json={'image': tacos})\n",
    "print(r)\n",
    "response = r.json()\n",
    "print(json.dumps(response, indent=4, sort_keys=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "burrito = 'https://www.exploreveg.org/files/2015/05/sofritas-burrito.jpeg'\n",
    "r = requests.post(scoring_uri, json={'image': tacos})\n",
    "print(r)\n",
    "response = r.json()\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scoring_uri"
   ]
  }
 ],
 "metadata": {
  "file_extension": ".py",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  },
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3
 },
 "nbformat": 4,
 "nbformat_minor": 2
}